/*
 * To change this license header, choose License Headers in Project Properties.
 * To change this template file, choose Tools | Templates
 * and open the template in the editor.
 */

/* 
 * File:   main.cpp
 * Author: user
 *
 * Created on June 5, 2017, 9:08 AM
 */

#include <cstdlib>
#include <iostream>

#include <unistd.h> 

#include "IceTransport.h"

using namespace std;

#define SDP_BUFFER_LEN 4000

#define WAIT_MILLI_SEC (100 * 1000)

int processKey(IceTransport* trans1, IceTransport* trans2);
int waitState(IceTransport* trans1, IceTransport* trans2, pj_ice_strans_state state);

int main(int argc, char** argv) {
    char sdp1[SDP_BUFFER_LEN] = {0};
    char sdp2[SDP_BUFFER_LEN] = {0};

    IceTransport* transport1 = new IceTransport();
    IceTransport* transport2 = new IceTransport();

    transport1->setOptions();
    transport2->setOptions();

    transport1->setRole('o');
    transport2->setRole('a');

    transport1->setName("transport1");
    transport2->setName("transport2");

    transport1->init();
    transport2->init();

    transport1->start();
    transport2->start();

    waitState(transport1, transport1, PJ_ICE_STRANS_STATE_READY);

    transport1->getLocalSDP(sdp1, SDP_BUFFER_LEN);
    transport2->getLocalSDP(sdp2, SDP_BUFFER_LEN);

    transport1->setRemoteSDP(sdp2);
    transport2->setRemoteSDP(sdp1);

    transport1->startNegotiation();
    transport2->startNegotiation();

    waitState(transport1, transport1, PJ_ICE_STRANS_STATE_RUNNING);

    processKey(transport1, transport2);

    transport1->stop();
    transport2->stop();

    transport1->deinit();
    transport2->deinit();

    return 0;
}

int processKey(IceTransport* trans1, IceTransport* trans2) {
    bool run = 1;

    while (run) {
        char str[8];

        fgets(str, sizeof(str), stdin);
        switch (str[0]) {
            case '1':
            {
                trans1->sendData(1, "data from t1 c1");
                break;
            }
            case '2':
            {
                trans1->sendData(2, "data from t1 c2");
                break;
            }
            case '3':
            {
                trans2->sendData(1, "data from t2 c1");
                break;
            }
            case '4':
            {
                trans2->sendData(2, "data from t2 c2");
                break;
            }
            case 's':
            {
                trans1->showIceInfo();
                break;
            }
            case 'd':
            {
                trans2->showIceInfo();
                break;
            }
            case 'q':
            {
                run = 0;
                break;
            }

        }
    }
}

int waitState(IceTransport* trans1, IceTransport* trans2, pj_ice_strans_state state)
{
    while (1) {
        usleep(WAIT_MILLI_SEC);
        if ((pj_ice_strans_get_state(trans1->icest) == state)
                &&(pj_ice_strans_get_state(trans2->icest) == state)
                ) {
            break;
        }
    }
    
    return 0;
}
